package cn.dansj.handler.interfaces;

import cn.dansj.annotation.MarkAsApi;
import cn.dansj.bean.CurrentThreadLocalUserInfo;
import cn.dansj.common.utils.json.DictMap;
import cn.dansj.common.utils.transfer.StringUtils;
import cn.dansj.common.utils.transfer.Transformation;
import cn.dansj.handler.InterfaceHandler;

import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
@MarkAsApi
public class LoginHistory extends InterfaceHandler {
    private static final DictMap sourceMap = DictMap.of("MicroMessenger", "微信浏览器", "Edg", "Edge浏览器", "Chrome", "Chrome浏览器", "Safari", "Safari浏览器");

    @Override
    public Object execute(Map<String, Object> paramsMap) {
        String sql = """
                select sequence, 
                      case url
                        when '/login' then
                         '<pc端浏览器>账号登录'
                        when '/reg/login' then
                         '<小程序端>账号登录'
                        when '/bindUserIdAndToken' then
                         '<pc端浏览器>扫码登录'
                        when '/reg/login/wxcode' then
                         '<小程序端>微信登录'
                        when '/h5_login' then
                         '<手机端>h5登录'
                         when '/exchangeToken' then
                         '控制台跳转登录'
                      end loginType,
                      case url
                        when '/login' then
                         1
                        when '/reg/login' then
                         2
                        when '/bindUserIdAndToken' then
                         3
                        when '/reg/login/wxcode' then
                         4
                        when '/h5_login' then
                         5
                        when '/exchangeToken' then
                         6
                      end loginFrom,
                      status,
                      crt_time loginTime,
                      user_agent userAgent
                 from x_request_log t
                where t.url in ('/login',
                                '/bindUserIdAndToken',
                                '/reg/login',
                                '/h5_login',
                                '/reg/login/wxcode',
                                '/exchangeToken')
                  and t.login_user = ?
                  and t.controller in ('cn.dansj.controller.LoginController','cn.dansj.controller.InterfaceRequestController')
                order by t.crt_time desc limit 20
                """;
        final List<Map<String, Object>> history = jdbcTemplate.queryForList(sql, CurrentThreadLocalUserInfo.getCurrentThreadLocalUserInfo().getCacheToken().getUsername());

        for (Map<String, Object> map : history) {
            String client = "";
            String clientType = "";
            final String userAgent = map.remove("useragent").toString();
            if (Transformation.toString(map.get("loginfrom")).equals("3")) {
                final String[] useragentSplit = StringUtils.split(userAgent, "|");
                final String mobileAgent = useragentSplit[0];
                final String pcAgent = useragentSplit[1];
                final String mobileClient = Transformation.patternGet(mobileAgent, "Android|iPhone");
                final String mobileClientType = Transformation.patternGet(mobileAgent, "MicroMessenger|Edg|Chrome|Safari");
                final String pcClient = Transformation.patternGet(pcAgent, "Windows|Linux|Unix|Mac");
                final String pcClientType = Transformation.patternGet(pcAgent, "MicroMessenger|Edg|Chrome|Safari");
                StringBuilder stringBuilder = new StringBuilder();
                final String mobileClientSource = sourceMap.getString(mobileClientType);
                client = stringBuilder.append(mobileClient).append(" ").append(Transformation.nvl(mobileClientSource.equals("微信浏览器") ? "微信小程序" : mobileClientSource, "其他客户端")).append(",")
                        .append(pcClient).append(" ").append(Transformation.nvl(sourceMap.get(pcClientType), "其他客户端")).toString();
            } else {
                final String _client = Transformation.patternGet(userAgent, "Android|iPhone|Windows|Linux|Unix|Mac");
                final String _clientType = Transformation.patternGet(userAgent, "MicroMessenger|Edg|Chrome|Safari");
                client = _client + " " + Transformation.nvl(sourceMap.get(_clientType), "其他客户端");
            }
            map.put("clientType", client);
        }

        return history;
    }
}